package sixth.instanceof关键字;

class A {
    public void fun1() {
        System.out.println("A--fun1()");
    }

    public void fun2() {
        System.out.println("A--fun2()");
    }
}

class B extends A {
    public void fun1() {
        // 覆写
        System.out.println("B--fun1()");
    }

    public void fun3() {
        System.out.println("B--fun3()");
    }
}

public class 验证instanceof的作用 {
    public static void main(String[] args) {
        // 向上转型实例化子类
        A a = new B();
        System.out.println("a instanceof A?：" + (a instanceof A));
        System.out.println("a instanceof B?：" + (a instanceof B));
        //通过A类的构造实例化A类对象
        A a1 = new A();
        System.out.println("a1 instanceof A?：" + (a1 instanceof A));
        System.out.println("a1 instanceof B?：" + (a1 instanceof B));
    }
}
/**
 * 通过子类实例化的对象同时是子类和父类的实例 所以可以直接进行向上或者向下转型
 * 但如果直接使用了父类实例化本类对象 则一定不再是子类实例了 所以是不能进行转换的
 */
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=65235:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 sixth.instanceof关键字.验证instanceof的作用
a instanceof A?：true
a instanceof B?：true
a1 instanceof A?：true
a1 instanceof B?：false

Process finished with exit code 0

 */